$(function () {
	//多选框
	$(document).on('change', 'input:checkbox.master', function(){
		if($(this).prop('checked')){
			$('input:checkbox.slaver').prop('checked', 'checked');
		}else{
			$('input:checkbox.slaver').prop('checked', '');
		}
	});
	
	//新增按钮
	$('.increase').click(function(){
		$('#infoQuantityAddModal').modal('show');
		$(".widget-caption").html("新增");
	});
	
	//启用
	$('#infoQuantityTable').on('click', 'a.btn-use', function(){
		var id = $(this).data('id');
		$.ajax({
			url : ctx + '/bizInfosBelong/updateRecord',
			data : {infosBelongId:id,enable:0},
			dataType : 'json',
			type : 'post',
			success : function(data){
				if(data.status != "success"){
					toastr.error(data.msg);
				}else{
					InitiateSimpleDataTable.init();
				}
			},
			error : function(){
				toastr.error("系统错误");
			}
		});
	});
	
	//禁用
	$('#infoQuantityTable').on('click', 'a.btn-nouse', function(){
		var id = $(this).data('id');
		$.ajax({
			url : ctx + '/bizInfosBelong/updateRecord',
			data : {infosBelongId:id,enable:1},
			dataType : 'json',
			type : 'post',
			success : function(data){
				if(data.status != "success"){
					toastr.error(data.msg);
				}else{
					InitiateSimpleDataTable.init();
				}
			},
			error : function(){
				toastr.error("系统错误");
			}
		});
		
	});
	
	//查看
	$('#infoQuantityTable').on('click', 'a.view', function(){
		
		var id = $(this).data('id');
		var name = $(this).data('name');
		var projects = $(this).data('projects');
		var departmentId = $(this).data('departmentid');
		var departmentName = $(this).data('departmentname');
		var brandSchool = $(this).data('brandschool');
		
		if(!isNaN(projects) &&　projects){
			projects = projects.toString();
		}
		
		$('.infoQuantityView input[name="infosBelongId"]').val(id);
		$('.infoQuantityView input[name="infosBelongName"]').val(name);
		if(projects)
			$('.infoQuantityView select[name="projects"]').selectpicker('val', projects.indexOf(",")?projects.split(','):projects);
		$('.infoQuantityView input[name="departmentId"]').val(departmentId);
		$('.infoQuantityView input[name="departmentName"]').val(departmentName);
		
		$('.infoQuantityView .selectpicker').attr('disabled', 'disabled');
		
		for(var i=0; i<brandSchool.length; ++i){
			$('.infoQuantityView .'+brandSchool[i].brand+'').selectpicker('val', brandSchool[i].school);
		}
		
		$('.infoQuantityView').modal('show');
	
	});
	
	//编辑
	$('#infoQuantityTable').on('click', 'a.edit', function(){
		$(".widget-caption").html("编辑");
		var id = $(this).data('id');
		var name = $(this).data('name');
		var projects = $(this).data('projects');
		var departmentId = $(this).data('departmentid');
		var departmentName = $(this).data('departmentname');
		var brandSchool = $(this).data('brandschool');
		var companyName = $(this).data('companyname');
		
		if(!companyName){
			$("#company_id").val(departmentName);
		}else{
			$("#company_id").val(companyName);
		}
		
		if(!isNaN(projects) && projects){
			projects = projects.toString();
		}
		
		$('.infoQuantityAdd input[name="infosBelongId"]').val(id);
		$('.infoQuantityAdd input[name="infosBelongName"]').val(name);
		if(projects)
			$('.infoQuantityAdd select[name="projects"]').selectpicker('val', projects.indexOf(",")?projects.split(','):projects);
		$('.infoQuantityAdd input[name="departmentId"]').val(departmentId);
		$('.infoQuantityAdd input[name="departmentName"]').val(departmentName);
		
		for(var i=0; i<brandSchool.length; ++i){
			$('#infoQuantityAdd .'+brandSchool[i].brand+'').selectpicker('val', brandSchool[i].school);
		}
		
		$('.infoQuantityAdd').modal('show');
		
	});
	
	
	//初始化项目select 
	$.ajax({
		url : ctx + '/bizProject/getAll',
		dataType : 'json',	
		type : 'post',
		success : function(data){
			if(data.status != "success"){
				toastr.error(data.msg);
			}else{
				var str = "";
				for(var i=0; i<data.list.length; ++i){
					str += "<option value='"+data.list[i].projectId+"'>"+data.list[i].fullName+"</option>";
				}
				$('#infoQuantityAddModal').find('select[name="projects"]').html(str);
				$('.infoQuantityView').find('select[name="projects"]').html(str);
				//$('#infoQuantityAddModal').selectpicker('refresh');
				$('.selectpicker').selectpicker();
			}
		},
		error : function(){
			toastr.error("系统错误");
		}
	}); 
	
	
	//初始化分校select 
	$.ajax({
        url: ctx + '/department/getAllOption',
        type: 'POST',
        data: {type : 3},
        dataType: 'json',
        success: function (data) {
    		var opt = "";
        	for(var i=0; i<data.list.length; i++){
        		opt += "<option value="+data.list[i].departmentId+">" + data.list[i].fullName+ "</option>";
        	}
        	
        	//初始化品牌
        	$.ajax({
                url: ctx + '/bizBrand/getAllOption',
                type: 'POST',
                dataType: 'json',
                success: function (data) {
            		var brandSchool = "";
                	for(var i=0; i<data.list.length; i++){
                		brandSchool += '<div class="form-group brand">'+
                            		'<div class="col-sm-2 col-md-offset-2">'+
                            			'<input type="hidden" name="brandSchools['+i+'].brand" class="form-control" value="'+data.list[i].brandId+'"/>'+
                        				'<input type="text" readonly="readonly" value="'+data.list[i].brandName+'" class="form-control"/>'+
        						     '</div>'+
        						     '<div class="col-sm-6 no-padding-left">'+
        						         '<select name="brandSchools['+i+'].school" class="form-control selectpicker '+data.list[i].brandId+'" multiple title="分校">'+opt+'</select>'+
        						     '</div>'+
        						 '</div>';
                	}
                	$('#infoQuantityAddModal .brand-before').after(brandSchool);
                	$('.infoQuantityView .brand-before').after(brandSchool);
                	$('.selectpicker').selectpicker();
                	
                },
                error: function (response) {
                	toastr.error("系统错误");
                }
            });
        	
        },
        error: function (response) {
        	toastr.error("系统错误");
        }
    });
	
	$('#infoQuantityAddModal').on('hidden.bs.modal', function () {
		
		//表单重置
		document.getElementById("infoQuantityAdd").reset();
		//from表单取消验证
		$('#infoQuantityAdd').data('bootstrapValidator').resetForm();
		//清空多选框
		$('#infoQuantityAdd .selectpicker').selectpicker('val', '');
		
		$('#infoQuantityAdd input[name="infosBelongId"]').val('');
		
	});
	
	//新增或编辑
	$('#infoQuantityAdd').bootstrapValidator({
		fields: {
			infosBelongName: {
	            validators: {
	                notEmpty: {
	                    message: '名称不能为空！'
	                }
	            }
	        }
		},
		submitHandler: function (validator, form, submitButton) {
			var $form = $(form);
			
			var options = $form.serialize();
			
			var projectsName = $('#infoQuantityAdd select[name="projects"]').siblings('button').attr('title');
			
			options = options + "&projectsName=" + projectsName;
			
			$.ajax({
				url : ctx + '/bizInfosBelong/addNewRecord',
				data : options,
				dataType : 'json',	
				type : 'post',
				success : function(data){
					if(data.status != "success"){
						toastr.error(data.msg);
					}else{
						$('#infoQuantityAddModal').modal('hide');
						InitiateSimpleDataTable.init();
					}
				},
				error : function(){
					toastr.error("系统错误");
				}
			}); 
		    
			return false;
		}
	});
	
	InitiateSimpleDataTable.init();
	
});


function retrieveData( sSource, aoData, fnCallback, oSettings ) {  
	
    aoData.push( { "name": "pageNum", "value": (Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength )+1) });
    aoData.push( { "name": "pageSize", "value": oSettings._iDisplayLength });  
    
    aoData.push({ "name": "enable", "value": "" });
    
    var searchVal = $('#searchVal').val();
    if(searchVal && searchVal.length != 0){
    	aoData.push({ "name": "searchVal", "value": searchVal });
    }
    
    $.ajax( {  
        "url": sSource,  
        "data": aoData,  
        "cache": false,  
        "dataType": 'json', 
        "type": "POST", 
        "success" :function(response) {  
        	fnCallback(response.returnObject);
        }  
    } );  
}  

//回车搜索
function search(){
	if(event.keyCode==13){
		InitiateSimpleDataTable.init();
	}
}


